package com.fulei.logic;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

public class FuLeiKeyOperate
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  public static byte controlPanelInfo;
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int ControlPanel()
  {
    return controlPanelInfo;
  }
  
  public static int ResetKey()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 10, 0, 1, 18, 0, 0, 1, -100, -19 };
      
      LogUtil.writeKeyLog("ResetKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("ResetKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -110) && 
          (ReadData[7] == 1)) {
          return_data = 0;
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("按键异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int QueryKey()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 10, 0, 1, 1, 0, 0, 1, -117, -19 };
      
      LogUtil.writeKeyLog("QueryKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 10);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("QueryKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -127))
        {
          controlPanelInfo = ReadData[6];
          switch (ReadData[7])
          {
          case 1: 
            return_data = 1;
            break;
          case 2: 
            return_data = 2;
            break;
          case 3: 
            return_data = 3;
            break;
          case 4: 
            return_data = 4;
            break;
          case 5: 
            return_data = 5;
            break;
          case 6: 
            return_data = 6;
            break;
          case 7: 
            return_data = 7;
            break;
          case 8: 
            return_data = 8;
            break;
          case 9: 
            return_data = 9;
            break;
          case 10: 
            return_data = 10;
            break;
          case 11: 
            return_data = 11;
            break;
          case 12: 
            return_data = 12;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("查询按键异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int FlashKey(int flashVal)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 2;
      SendData[5] = 0;
      SendData[6] = 0;
      SendData[7] = ((byte)flashVal);
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writeKeyLog("FlashKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("FlashKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("闪灯异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int GoodsFlashKey()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 10, 0, 1, 3, 0, 0, 1, -115, -19 };
      LogUtil.writeKeyLog("GoodsFlashKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("GoodsFlashKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("出货口闪灯异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int PayoutFlashKey()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 10, 0, 1, 4, 0, 0, 1, -114, -19 };
      LogUtil.writeKeyLog("PayoutFlashKeySend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 100, 2);
      if (ReadDataLength < 1) {
        return -1;
      }
      LogUtil.writeKeyLog("PayoutFlashKeyRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("找零闪灯异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static byte SumByte(byte[] data, int offset, int length)
  {
    int Result = 0;
    for (int i = offset; i <= offset + length; i++) {
      Result += data[i];
    }
    return (byte)Result;
  }
}
